﻿using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp_MvvmLight.Base;

namespace WpfApp_MvvmLight.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Messenger.Default.Register<string>(this,ExecuteMessage1);

            Messenger.Default.Register<WindowCom>(this, ExecuteMessage2);

            //通过AAA关键字调用通知
            Messenger.Default.Register<string>(this, "AAA", args => 
            {
                
            });
        }


        private void ExecuteMessage1(string args)
        { 
            SubWindow subWindow = new SubWindow() { Owner = this };
            subWindow.ShowDialog();
            
        }

        /// <summary>
        /// 通过委托互相调用
        /// </summary>
        /// <param name="windowCom"></param>
        private void ExecuteMessage2(WindowCom windowCom)
        {
            SubWindow subWindow = new SubWindow() { Owner = this };
            bool _ret = subWindow.ShowDialog() == true;

            windowCom.SubmitAction?.Invoke(_ret);
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            //窗口关闭取消注册
            Messenger.Default.Unregister<string>(this);
            Messenger.Default.Unregister<WindowCom>(this);
            Messenger.Default.Unregister("AAA");
        }
    }
}
